﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Microsoft Corp." file="SqlAzureManagementApiTests.cs">
//   All rights reserved. See License.txt in the project root for license information.
// </copyright>
// 
// --------------------------------------------------------------------------------------------------------------------
namespace AzureManagementTests
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;

    using AzureManagement;
    using AzureManagement.SqlAzureDataContracts;

    using AzureManagementTests.TestSupport;

    using Xunit;

    public class SqlAzureManagementApiTests
    {
        private const string Prefix = "sqltest";

        private const string BaseUri = Prefix + "://management.database.windows.net:8443/";

        private const string ManagementCertificateThumbprint = "f5af408cdece42fd6b5f35c8889c0e22ac1c5ec2";

        private const string ApiVersion = "1.0";

        private const string SubscriptionId = "b4249f2a-2378-4c3e-8e5a-113145d7f1c0";

        private static readonly SqlAzureManagementTestWebRequestCreate TestWebRequestCreate;

        static SqlAzureManagementApiTests()
        {
            TestWebRequestCreate = new SqlAzureManagementTestWebRequestCreate
            {
                BaseUri = BaseUri, 
                SubscriptionId = SubscriptionId, 
                ApiVersion = ApiVersion, 
                ManagementCertificates = new List<string>
                {
                    ManagementCertificateThumbprint
                },
                Servers = new List<SqlAzureServerMock>
                {
                    new SqlAzureServerMock
                    {
                        Server = new SqlAzureServer
                        {
                            Name = "EC70FF30-BF8C-424D-9CB5-E2803115B711", 
                            AdministratorLogin = "adminlogin1", 
                            Location = "West US"
                        },
                        Rules = new List<FirewallRuleMock>
                        {
                            new FirewallRuleMock
                            {
                                Name = "Test rule 1", 
                                Rule = new FirewallRule
                                {
                                    StartIpAddress = "213.199.160.0", 
                                    EndIpAddress = "213.199.175.255"
                                }
                            }
                        }
                    },
                    new SqlAzureServerMock
                    {
                        Server = new SqlAzureServer
                        {
                            Name = "DFD01BB0-4948-43A2-8974-E67B18985294", 
                            AdministratorLogin = "adminlogin2", 
                            Location = "North US"
                        },
                        Rules = new List<FirewallRuleMock>
                        {
                            new FirewallRuleMock
                            {
                                Name = "Test rule 1", 
                                Rule = new FirewallRule
                                {
                                    StartIpAddress = "213.199.160.0", 
                                    EndIpAddress = "213.199.175.255"
                                }
                            }
                        }
                    },
                    new SqlAzureServerMock
                    {
                        Server = new SqlAzureServer
                        {
                            Name = "E5E32CB2-8E8F-42FA-84E3-40EA7C873072", 
                            AdministratorLogin = "adminlogin2", 
                            Location = "East US"
                        },
                        Rules = new List<FirewallRuleMock>
                        {
                            new FirewallRuleMock
                            {
                                Name = "Test rule 1", 
                                Rule = new FirewallRule
                                {
                                    StartIpAddress = "213.199.160.0", 
                                    EndIpAddress = "213.199.175.255"
                                }
                            }
                        }
                    }
                }
            };

            WebRequest.RegisterPrefix(Prefix, TestWebRequestCreate);
        }

        [Fact]
        public void CanCreateSqlAzureServer()
        {
            var serviceContext = new AzureServiceContext(
                                        new TestServiceCredentials(SubscriptionId, ManagementCertificateThumbprint), 
                                        new Uri(BaseUri), 
                                        ApiVersion);

            var azureService = new SqlAzureManagementClient(serviceContext);

            const string Login = "adminLogin";
            const string Pass = "adminPassword";
            const string Location = "West US";
            var serverName = azureService.CreateSqlAzureServer(Login, Pass, Location).Result;

            var server = TestWebRequestCreate.Servers.First(x => x.Server.Name.Equals(serverName)).Server;
            Assert.Equal(server.AdministratorLogin, Login);
            Assert.Equal(server.Location, Location);
        }

        [Fact]
        public void CanListSqlAzureServers()
        {
            var serviceContext = new AzureServiceContext(
                                        new TestServiceCredentials(SubscriptionId, ManagementCertificateThumbprint), 
                                        new Uri(BaseUri), 
                                        ApiVersion);

            var azureService = new SqlAzureManagementClient(serviceContext);

            var list = azureService.ListSqlAzureServers().Result;
            Assert.Equal(list.Count, TestWebRequestCreate.Servers.Count);
        }

        [Fact]
        public void CanDeleteSqlAzureServer()
        {
            var serviceContext = new AzureServiceContext(
                                        new TestServiceCredentials(SubscriptionId, ManagementCertificateThumbprint),
                                        new Uri(BaseUri),
                                        ApiVersion);

            var azureService = new SqlAzureManagementClient(serviceContext);

            var serverName = azureService.CreateSqlAzureServer("adminLogin", "adminPassword", "West US").Result;

            azureService.DeleteSqlAzureServer(serverName).Wait();

            Assert.False(TestWebRequestCreate.Servers.Any(x => x.Server.Name.Equals(serverName)));
        }

        [Fact]
        public void CanCreateFirewallRule()
        {
            var serviceContext = new AzureServiceContext(
                                        new TestServiceCredentials(SubscriptionId, ManagementCertificateThumbprint),
                                        new Uri(BaseUri),
                                        ApiVersion);

            var azureService = new SqlAzureManagementClient(serviceContext);

            var sqlServerMock = TestWebRequestCreate.Servers.First();

            const string RuleName = "Test firewall rule 2";
            const string StartIp = "213.199.184.0";
            const string EndIp = "213.199.184.20";
            azureService.CreateFirewallRule(sqlServerMock.Server.Name, RuleName, StartIp, EndIp).Wait();

            var ruleMock = sqlServerMock.Rules.First(x => x.Name.Equals(RuleName));
            Assert.Equal(ruleMock.Rule.StartIpAddress, StartIp);
            Assert.Equal(ruleMock.Rule.EndIpAddress, EndIp);
        }

        [Fact]
        public void CanCreateFirewallRuleWithIpDetect()
        {
            var serviceContext = new AzureServiceContext(
                                        new TestServiceCredentials(SubscriptionId, ManagementCertificateThumbprint),
                                        new Uri(BaseUri),
                                        ApiVersion);

            var azureService = new SqlAzureManagementClient(serviceContext);

            var sqlServerMock = TestWebRequestCreate.Servers.First();

            const string RuleName = "Test firewall rule 3";
            azureService.CreateFirewallRuleWithIpDetect(sqlServerMock.Server.Name, RuleName).Wait();

            Assert.True(sqlServerMock.Rules.Any(x => x.Name.Equals(RuleName)));
        }
    }
}